//
// Created by work on 24-6-25.
//
#include <iot_model_device/CModelInfo.h>
#include <server/CModelApi.h>
#include "iot_model_device/device_model_mgr/CUserMgr.h"
#include "iot_model_device/device_model_mgr/CModelMgr.h"
#include <iot_model_device/device_model_mgr/CProtocolMgr.h>
#include "crow.h"
#include <server/server.h>
#include <os/miscellany/stddef.h>

namespace iotplat
{
    CModelApi::CModelApi()
    {
    }

    void CModelApi::start()
    {
        addModel();
        delModel();
        changeModel();
        getModel();
        getModels();

        addModelYx();
        delModelYx();
        changeModelYx();
        getModelYx();
        getModelYxs();

        addModelYc();
        delModelYc();
        changeModelYc();
        getModelYc();
        getModelYcs();

        addModelYk();
        delModelYk();
        changeModelYk();
        getModelYk();
        getModelYks();

        addModelParameter();
        delModelParameter();
        changeModelParameter();
        getModelParameter();
        getModelParameters();

        addDevice();
        getDevicesByModel();

        /*modbus映射接口*/
        addModbusMap();
        getModbusMaps();
        getModbusMapYxs();
        changeModbusMapYxs();
        getModbusMapYcs();
        changeModbusMapYcs();
        getModbusMapYks();
        changeModbusMapYks();
        getModbusMapParameters();
        changeModbusMapParameters();
    }

    void CModelApi::addModel() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/addModel").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CModelInfo model;
            model.fromJson(body);

            if(!CModelMgr::ins().addModel(model)) {
                REPLY_FAILED_REQUEST("添加模型失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("模型添加成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::delModel() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/delModel").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::mdoelFieldName(MODEL_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::mdoelFieldName(MODEL_ENUM::EName)].asString();

            if(!CModelMgr::ins().delModel(userid, modelName)) {
                REPLY_FAILED_REQUEST("删除模型失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("删除模型成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::changeModel() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/changeModel").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CModelInfo model;
            model.fromJson(body);

            if(!CModelMgr::ins().changeModel(model)) {
                REPLY_FAILED_REQUEST("添加修改失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("模型修改成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::getModel() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModel").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::mdoelFieldName(MODEL_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::mdoelFieldName(MODEL_ENUM::EName)].asString();
            std::string type = body[TYPE].asString();

            CModelInfo model;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModel(userid, modelName, model)) {
                    REPLY_FAILED_REQUEST("读取数据库模型失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModel(userid, modelName, model)) {
                    REPLY_FAILED_REQUEST("读取实时库模型失败!");
                }
            }

            Json::Value resJson = model.toJson();
            resJson[REQ_STATE] =  successJson("模型读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModels() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModels").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::mdoelFieldName(MODEL_ENUM::EUser)].asString();
            std::string type = body[TYPE].asString();

            std::vector<CModelInfo> models;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModels(userid, models)) {
                    REPLY_FAILED_REQUEST("读取数据库模型列表失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModels(userid, models)) {
                    REPLY_FAILED_REQUEST("读取实时库模型列表失败!");
                }
            }

            Json::Value resJson;
            Json::Value arr;
            for(int index = 0; index < models.size(); ++index) {
                arr.insert(index, models[index].toJson());
            }
            resJson["models"] = arr;
            resJson[REQ_STATE] =  successJson("模型读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::addModelYx() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/addModelYx").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelYxInfo yx;
            yx.fromJson(body);

            std::cout << yx.toJson().toStyledString() << std::endl;

            if(!CModelMgr::ins().addModelYx(yx)) {
                REPLY_FAILED_REQUEST("添加遥信失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("遥信添加成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::delModelYx() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/delModelYx").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[baseSigFieldName(BASE_SIG_ENUM::EName)].asString();

            if(!CModelMgr::ins().delModelYx(userid, modelName, name)) {
                REPLY_FAILED_REQUEST("删除遥信失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("删除遥信成功！").toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModelYx() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/changeModelYx").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelYxInfo yx;
            yx.fromJson(body);

            if(!CModelMgr::ins().changeModelYx(yx)) {
                REPLY_FAILED_REQUEST("修改遥信失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("遥信修改成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::getModelYx() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelYx").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EName)].asString();
            std::string type = body[TYPE].asString();

            CRtModelYxInfo yx;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelYx(userid, modelName, name, yx)) {
                    REPLY_FAILED_REQUEST("读取数据库遥信失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelYx(userid, modelName, name, yx)) {
                    REPLY_FAILED_REQUEST("读取实时库遥信失败!");
                }
            }

            Json::Value resJson = yx.toJson();
            resJson[REQ_STATE] =  successJson("遥信读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModelYxs() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelYxs").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string type = body[TYPE].asString();

            std::vector<CRtModelYxInfo>  yxs;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelYxs(userid,modelName,yxs)) {
                    REPLY_FAILED_REQUEST("读取数据库遥信失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelYxs(userid, modelName,yxs)) {
                    REPLY_FAILED_REQUEST("读取实时库遥信失败!");
                }
            }

            Json::Value resJson;
            Json::Value arr;
            for(int index = 0; index < yxs.size(); ++index) {
                arr.insert(index, yxs[index].toJson());
            }
            resJson["yxs"] = arr;
            resJson[REQ_STATE] =  successJson("遥信读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::addModelYc() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/addModelYc").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelYcInfo yc;
            yc.fromJson(body);

            if(!CModelMgr::ins().addModelYc(yc)) {
                REPLY_FAILED_REQUEST("添加遥测失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("遥测添加成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::delModelYc() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/delModelYc").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[baseSigFieldName(BASE_SIG_ENUM::EName)].asString();

            if(!CModelMgr::ins().delModelYc(userid, modelName, name)) {
                REPLY_FAILED_REQUEST("删除遥测失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("删除遥测成功！").toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModelYc() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/changeModelYc").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelYcInfo yc;
            yc.fromJson(body);

            if(!CModelMgr::ins().changeModelYc(yc)) {
                REPLY_FAILED_REQUEST("修改遥测失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("遥测修改成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::getModelYc() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelYc").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EName)].asString();
            std::string type = body[TYPE].asString();

            CRtModelYcInfo yc;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelYc(userid, modelName, name, yc)) {
                    REPLY_FAILED_REQUEST("读取数据库遥测失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelYc(userid, modelName, name, yc)) {
                    REPLY_FAILED_REQUEST("读取实时库遥测失败!");
                }
            }

            Json::Value resJson = yc.toJson();
            resJson[REQ_STATE] =  successJson("遥测读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModelYcs() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelYcs").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string type = body[TYPE].asString();

            std::vector<CRtModelYcInfo>  Ycs;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelYcs(userid,modelName,Ycs)) {
                    REPLY_FAILED_REQUEST("读取数据库遥测失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelYcs(userid, modelName,Ycs)) {
                    REPLY_FAILED_REQUEST("读取实时库遥测失败!");
                }
            }

            Json::Value resJson;
            Json::Value arr;
            for(int index = 0; index < Ycs.size(); ++index) {
                arr.insert(index, Ycs[index].toJson());
            }
            resJson["Ycs"] = arr;
            resJson[REQ_STATE] =  successJson("遥测读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::addModelYk() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/addModelYk").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelYkInfo yk;
            yk.fromJson(body);

            if(!CModelMgr::ins().addModelYk(yk)) {
                REPLY_FAILED_REQUEST("添加遥控失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("遥控添加成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::delModelYk() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/delModelYk").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[baseSigFieldName(BASE_SIG_ENUM::EName)].asString();

            if(!CModelMgr::ins().delModelYk(userid, modelName, name)) {
                REPLY_FAILED_REQUEST("删除遥控失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("删除遥控成功！").toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModelYk() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/changeModelYk").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelYkInfo yk;
            yk.fromJson(body);

            if(!CModelMgr::ins().changeModelYk(yk)) {
                REPLY_FAILED_REQUEST("修改遥控失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("遥控修改成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::getModelYk() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelYk").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EName)].asString();
            std::string type = body[TYPE].asString();

            CRtModelYkInfo yk;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelYk(userid, modelName, name, yk)) {
                    REPLY_FAILED_REQUEST("读取数据库遥控失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelYk(userid, modelName, name, yk)) {
                    REPLY_FAILED_REQUEST("读取实时库遥控失败!");
                }
            }

            Json::Value resJson = yk.toJson();
            resJson[REQ_STATE] =  successJson("遥控读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModelYks() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelYks").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string type = body[TYPE].asString();

            std::vector<CRtModelYkInfo>  Yks;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelYks(userid,modelName,Yks)) {
                    REPLY_FAILED_REQUEST("读取数据库遥控失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelYks(userid, modelName,Yks)) {
                    REPLY_FAILED_REQUEST("读取实时库遥控失败!");
                }
            }

            Json::Value resJson;
            Json::Value arr;
            for(int index = 0; index < Yks.size(); ++index) {
                arr.insert(index, Yks[index].toJson());
            }
            resJson["yks"] = arr;
            resJson[REQ_STATE] =  successJson("遥控读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::addModelParameter() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/addModelParameter").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelParameterInfo parameter;
            parameter.fromJson(body);

            if(!CModelMgr::ins().addModelParameter(parameter)) {
                REPLY_FAILED_REQUEST("添加参数失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("参数添加成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::delModelParameter() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/delModelParameter").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[baseSigFieldName(BASE_SIG_ENUM::EName)].asString();

            if(!CModelMgr::ins().delModelParameter(userid, modelName, name)) {
                REPLY_FAILED_REQUEST("删除参数失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("删除参数成功！").toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModelParameter() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/changeModelParameter").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtModelParameterInfo parameter;
            parameter.fromJson(body);

            if(!CModelMgr::ins().changeModelParameter(parameter)) {
                REPLY_FAILED_REQUEST("修改参数失败!");
            }

            res.set_header("Content-Type", "application/json");
            res.body = successJson("参数修改成功！").toStyledString().c_str();
            res.end();
        });
    }

    void CModelApi::getModelParameter() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelParameter").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string name = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EName)].asString();
            std::string type = body[TYPE].asString();

            CRtModelParameterInfo parameter;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelParameter(userid, modelName, name, parameter)) {
                    REPLY_FAILED_REQUEST("读取数据库参数失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelParameter(userid, modelName, name, parameter)) {
                    REPLY_FAILED_REQUEST("读取实时库参数失败!");
                }
            }

            Json::Value resJson = parameter.toJson();
            resJson[REQ_STATE] =  successJson("参数读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModelParameters() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getModelParameters").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string type = body[TYPE].asString();

            std::vector<CRtModelParameterInfo>  Parameters;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDataBaseModelParameters(userid,modelName,Parameters)) {
                    REPLY_FAILED_REQUEST("读取数据库参数失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtModelParameters(userid, modelName,Parameters)) {
                    REPLY_FAILED_REQUEST("读取实时库参数失败!");
                }
            }

            Json::Value resJson;
            Json::Value arr;
            for(int index = 0; index < Parameters.size(); ++index) {
                arr.insert(index, Parameters[index].toJson());
            }
            resJson["parameters"] = arr;
            resJson[REQ_STATE] =  successJson("参数读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::addDevice() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/addDevice").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            CRtDeviceInfo info;
            info.fromJson(body);

            if(!CModelMgr::ins().addDevice(info)) {
                REPLY_FAILED_REQUEST("添加失败!");
            }

            Json::Value resJson;
            resJson[REQ_STATE] =  successJson("设备添加成功！").toStyledString().c_str();
            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::delDevice() {

    }

    void CModelApi::changeDevice() {

    }

    void CModelApi::getDevice() {

    }

    void CModelApi::getDevicesByModel() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/model/getDevicesByModel").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string type = body[TYPE].asString();

            std::vector<CRtDeviceInfo>  devs;

            if(DATABASE == type) {
                if(!CModelMgr::ins().getDevicesByModel(userid,modelName,devs)) {
                    REPLY_FAILED_REQUEST("读取数据库设备失败!");
                }
            } else if(RT == type) {
                if(!CModelMgr::ins().getRtDevicesByModel(userid, modelName,devs)) {
                    REPLY_FAILED_REQUEST("读取实时库设备失败!");
                }
            }

            Json::Value resJson;
            Json::Value arr;
            for(int index = 0; index < devs.size(); ++index) {
                arr.insert(index, devs[index].toJson());
            }
            resJson["devs"] = arr;
            resJson[REQ_STATE] =  successJson("设备列表读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModbusMaps() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/getModbusMaps").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();

            Json::Value resJson;
            if(!CProtocolMgr::ins().getModbusMaps(userid, modelName,resJson))
                REPLY_FAILED_REQUEST("读取模型映射失败!");

            resJson[REQ_STATE] =  successJson("模型modbus映射读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::addModbusMap() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/addModbusMap").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            if(!CProtocolMgr::ins().addModbusMap(body))
                REPLY_FAILED_REQUEST("添加modbus映射失败!");

            Json::Value resJson;
            resJson[REQ_STATE] =  successJson("添加模型modbus映射成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::delModbusMap() {
    }

    void CModelApi::getModbusMapYxs() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/getModbusMapYxs").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string map_id = body["map_id"].asString();

            Json::Value resJson;
            if(!CProtocolMgr::ins().getModbusMapYxs(map_id,userid, modelName,resJson))
                REPLY_FAILED_REQUEST("读取模型遥信modbus映射失败!");

            resJson[REQ_STATE] =  successJson("模型modbus遥信映射读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModbusMapYxs() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/changeModbusMapYxs").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string map_id = body["map_id"].asString();
            Json::Value errorInfo;
            if(!CProtocolMgr::ins().changeModbusMapYxs(map_id,body["mapsigs"], errorInfo)) {
                REPLY_FAILED_REQUEST("遥信映射修改失败!");
            }

            Json::Value resJson;
            resJson[REQ_STATE] =  successJson("遥信映射修改成功！").toStyledString().c_str();
            resJson["faild_info"] = errorInfo.toStyledString().c_str();
            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModbusMapYcs() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/getModbusMapYcs").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string map_id = body["map_id"].asString();

            Json::Value resJson;
            if(!CProtocolMgr::ins().getModbusMapYcs(map_id,userid, modelName,resJson))
                REPLY_FAILED_REQUEST("读取模型遥测modbus映射失败!");

            resJson[REQ_STATE] =  successJson("模型modbus遥测映射读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModbusMapYcs() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/changeModbusMapYcs").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string map_id = body["map_id"].asString();
            Json::Value errorInfo;
            if(!CProtocolMgr::ins().changeModbusMapYcs(map_id,body["mapsigs"], errorInfo)) {
                REPLY_FAILED_REQUEST("遥测映射修改失败!");
            }

            Json::Value resJson;
            resJson[REQ_STATE] =  successJson("遥测映射修改成功！").toStyledString().c_str();
            resJson["faild_info"] = errorInfo.toStyledString().c_str();
            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModbusMapYks() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/getModbusMapYks").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string map_id = body["map_id"].asString();

            Json::Value resJson;
            if(!CProtocolMgr::ins().getModbusMapYks(map_id,userid, modelName,resJson))
                REPLY_FAILED_REQUEST("读取模型遥控modbus映射失败!");

            resJson[REQ_STATE] =  successJson("模型modbus遥测映射读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModbusMapYks() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/changeModbusMapYks").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string map_id = body["map_id"].asString();
            Json::Value errorInfo;
            if(!CProtocolMgr::ins().changeModbusMapYks(map_id,body["mapsigs"], errorInfo)) {
                REPLY_FAILED_REQUEST("遥控映射修改失败!");
            }

            Json::Value resJson;
            resJson[REQ_STATE] =  successJson("遥控映射修改成功！").toStyledString().c_str();
            resJson["faild_info"] = errorInfo.toStyledString().c_str();
            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::getModbusMapParameters() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/getModbusMapParameters").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string userid = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EUser)].asString();
            std::string modelName = body[iotplat::baseSigFieldName(BASE_SIG_ENUM::EModel_name)].asString();
            std::string map_id = body["map_id"].asString();

            Json::Value resJson;
            if(!CProtocolMgr::ins().getModbusMapParameters(map_id,userid, modelName,resJson))
                REPLY_FAILED_REQUEST("读取模型参数modbus映射失败!");

            resJson[REQ_STATE] =  successJson("模型modbus参数映射读取成功！").toStyledString().c_str();

            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }

    void CModelApi::changeModbusMapParameters() {
        CROW_ROUTE(CSimpleAppSingleton::getInstance(), "/api/protocol/changeModbusMapParameters").methods("POST"_method)([](const crow::request& req, crow::response& res) {
            Json::Value body;
            PSRSE_JSON_REQUEST(body);

            if(!CUserMgr::ins().checkToken(req.get_header_value(TOKEN))){
                REPLY_FAILED_REQUEST("令牌过期，请重新登陆");
            }

            std::string map_id = body["map_id"].asString();
            Json::Value errorInfo;
            if(!CProtocolMgr::ins().changeModbusMapParameters(map_id,body["mapsigs"], errorInfo)) {
                REPLY_FAILED_REQUEST("映射修改失败!");
            }

            Json::Value resJson;
            resJson[REQ_STATE] =  successJson("参数映射修改成功！").toStyledString().c_str();
            resJson["faild_info"] = errorInfo.toStyledString().c_str();
            res.set_header("Content-Type", "application/json");
            res.body = resJson.toStyledString();
            res.end();
        });
    }
}
